﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmLoaiVt : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmLoaiVatTuEvent;
        int State = 0;
        DmLoaiVatTuEntity dmLoaiVatTuEntity = null;
        DmLoaiVatTuBusiness dmLoaiVatTuBusiness = new DmLoaiVatTuBusiness();
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();

        public frmCapNhatDmLoaiVt(int state)
        {
            InitializeComponent();
            this.State = state;
        }

        public frmCapNhatDmLoaiVt(int state, DmLoaiVatTuEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmLoaiVatTuEntity = obj;

            txtMaLoaiVatTu.Text = dmLoaiVatTuEntity.MaLoaiVatTu;
            txtTenLoaiVatTu.Text = dmLoaiVatTuEntity.TenLoaiVatTu;
            txtMoTa.Text = dmLoaiVatTuEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmLoaiVatTuEntity.TrangThaiSuDung;
            txtMaLoaiVatTu.Enabled = false;
        }

        private void frmCapnhatDMLoaiVT_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaLoaiVatTu, "Mã loại vật tư") == false) return;

            dmLoaiVatTuEntity = new DmLoaiVatTuEntity()
            {
                MaLoaiVatTu = txtMaLoaiVatTu.Text,
                TenLoaiVatTu = txtTenLoaiVatTu.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmLoaiVatTuBusiness.GetDmLoaiVatTu(txtMaLoaiVatTu.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaLoaiVatTu, "Mã loại vật tư") == false) return;
                }

                dmLoaiVatTuBusiness.InsertDmLoaiVatTu(dmLoaiVatTuEntity);
                Validation.ClearControl(this);
                txtMaLoaiVatTu.Focus();
            }
            else
            {
                dmLoaiVatTuBusiness.EditDmLoaiVatTu(dmLoaiVatTuEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmLoaiVatTuEvent != null)
                dmLoaiVatTuEvent(sender, e);
        }

        private void frmCapnhatDMLoaiVT_Load(object sender, EventArgs e)
        {
        }

    }
}